package cm.iccsoft.android.telecomcost.ui;

import cm.iccsoft.android.telecomcost.DatabaseManager;
import cm.iccsoft.android.telecomcost.R;
import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.activities.ZonesListActivity;
import cm.iccsoft.android.telecomcost.entities.Zone;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ZoneDialog extends Dialog {
	
	private DatabaseManager db;
	private Button btnCreateZone;
	private Button btnCloseZoneDialog;
	private EditText txtName;	
	private Zone _zone = null;
	private boolean shouldRefreshZoneList = false;

	/**
	 * @param context
	 * @param cancelable
	 * @param cancelListener
	 */
	public ZoneDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);		
	}

	/**
	 * @param context
	 * @param theme
	 */
	public ZoneDialog(Context context, int theme) {
		super(context, theme);		
	}

	public ZoneDialog(Context context,Zone zone) {
		super(context);		
		_zone = zone;
	}	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i("EditZoneDialog","creating options menu for zone : ");
	    MenuInflater inflater = getOwnerActivity().getMenuInflater();
	    inflater.inflate(R.menu.menu_zone, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		
	    switch (item.getItemId()) {
	    case R.id.menu_item_save_zone:	
	    	saveZone();
	        return true;	        
	    case R.id.menu_item_close_zone_dialog:	
	    	Log.i("EditZoneDialog","closing the dialog");
			hide();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("EditZoneDialog", "Creating the dialog...");
		super.onCreate(savedInstanceState);

		db = TelecomCost.getDatabaseManager();
		setContentView(R.layout.edit_zone);		
		setTitle("Edit a Zone");

		btnCreateZone = (Button) findViewById(R.id.btnSave);
		btnCloseZoneDialog = (Button) findViewById(R.id.btnClose);
		txtName = (EditText) findViewById(R.id.editText1);
		
		txtName.setText(_zone != null ? _zone.getName() : "");

		btnCreateZone.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				saveZone();
			}						
		});

		btnCloseZoneDialog.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {	
				closeDialog();
			}
		});
		
		Log.i("EditZoneDialog","Dialog ohne bundle created....");
	}	
	
	protected void closeDialog() {
		Log.i("EditZoneDialog","closing the dialog");
		hide();
		if(shouldRefreshZoneList)
		{
			ZonesListActivity activity = (ZonesListActivity)getOwnerActivity();
			activity.bindZones();
		}
		
		shouldRefreshZoneList = false;
	}

	private void saveZone() {
		String name = txtName.getText().toString();

		if (name.length() == 0) {
			Toast.makeText(getOwnerActivity(),
					"Please specify the name of the zone", 3).show();
		} else {
			ZonesListActivity activity = (ZonesListActivity)getOwnerActivity();			
			Zone zone = activity.getZone(name);
			if(zone != null)
			{
				Toast.makeText(getOwnerActivity(),
						"a zone with the same name already exists", 3).show();
				return;
			}
						
			_zone = _zone == null ? new Zone(txtName.getText().toString()) : _zone;
			_zone.setId(db.getNextId());									
						
			Toast.makeText(getOwnerActivity(),db.saveZone(_zone,-1) ?
					"zone saved !!!" : "could not save this zone", 3).show();
			shouldRefreshZoneList = true;
			resetDialog();
		}
	}

	private void resetDialog() {
		_zone = null;
		txtName.setText("");
	}

	@Override
	public void show() {
		Log.i("EditZoneDialog", "showing the dialog...");
		onCreate(null);
		super.show();
		Log.i("EditZoneDialog", "dialog on screen...");
	}
	
	@Override
	protected void onStart() {
		Log.i("EditZoneDialog", "Starting the dialog...");
		super.onStart();
		Log.i("EditZoneDialog", "dialog started...");
	}

	@Override
	protected void onStop() {
		Log.i("EditZoneDialog", "Stopping the dialog...");
		super.onStop();
		Log.i("EditZoneDialog", "dialog stopped...");
	}

}
